package com.sk.waste.app.adapters

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.lc.smc.lib.interfaces.LCInterface
import com.lc.smc.lib.utils.LCBaseUtil
import com.sk.waste.app.R
import com.sk.waste.app.application.MyApplication
import com.sk.waste.app.config.NetWorkConfig
import com.sk.waste.app.models.MaterialModel
import com.sk.waste.app.models.MerchantlModel
import com.sk.waste.app.models.UserModel
import com.sk.waste.app.utils.BaseUtil

/**
 *  @time 2025/01/07  15:37
 *  @desc 商家
 */
class MerchantAdapter(val context: Activity, val type: Int, val list: List<MerchantlModel>, val callback: LCInterface.LcCallBack?) :
    RecyclerView.Adapter<MerchantAdapter.ViewHolder>() {
    var userInfo: UserModel? = null

    init {
        userInfo = MyApplication.instance.getUserInfo()
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MerchantAdapter.ViewHolder {
        if (type == 0)// 0首页商家展示，1 商家列表
            return ViewHolder(LayoutInflater.from(context).inflate(R.layout.adapter_merchant, parent, false))
        else
            return ViewHolder(LayoutInflater.from(context).inflate(R.layout.adapter_merchant_list, parent, false))
    }

    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, @SuppressLint("RecyclerView") position: Int) {

        val item = list[position]
        holder.ttvName.text = item.account
        if (!LCBaseUtil.isNull(item.nickName)) {
            holder.ttvName.text = item.nickName
        }
        holder.ttvContent.text = item.companyIntroduction
        if (!LCBaseUtil.isNull(item.address)) {
            holder.ttvAdress.text = item.address
        }
//        if (userInfo?.isVip() == true) {
//            holder.ttvPhone.text = item.phone
//        } else {
//            holder.ttvPhone.text = BaseUtil.getPhoneHideStr(item.phone)
//        }

//        holder.ttvPhone.text = item.phone
//        if (type == 0) {
//            holder.ttvContent.text = item.companyIntroduction
//            if (!LCBaseUtil.isNull(item.address)) {
//                holder.ttvAdress.text = item.address
//            }
//        } else {
//            holder.ttvContent.text = item.companyIntroduction
//            holder.ttvAdress.text = "地址："+item.address
//        }
        if (!LCBaseUtil.isNull(item.avatar)) {
            val path = NetWorkConfig.NET_URL_ROOT + NetWorkConfig.NET_URL_USER_AVATAR + item.avatar
            Glide.with(context).load(path).transform(CenterCrop(), RoundedCorners(20)).error(R.mipmap.img_item_default)
                .into(holder.imgAvatar);
        }

//        holder.ttvPhone.tag = position
//        holder.ttvPhone.setOnClickListener {
//            if (userInfo?.isVip() == true) {
//                val cp = holder.ttvPhone.tag as Int
//                BaseUtil.callPhone(context, list[cp].phone)
//            }
//        }

        holder.rootView.tag = position
        holder.rootView.setOnClickListener {
            val currentSelect = holder.rootView.tag as Int
            callback!!.callBack(currentSelect)
        }
    }

    fun mNotifyDataSetChanged() {
        userInfo = MyApplication.instance.getUserInfo()
        notifyDataSetChanged()
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val imgAvatar: ImageView = view.findViewById(R.id.item_merchant_icon)
        val ttvName: TextView = view.findViewById(R.id.item_merchant_name)
//        val ttvPhone: TextView = view.findViewById(R.id.item_merchant_phone)
        val ttvContent: TextView = view.findViewById(R.id.item_merchant_content)
        val ttvAdress: TextView = view.findViewById(R.id.item_merchant_adress)
        val rootView: ConstraintLayout = view.findViewById(R.id.view_top_layout)
    }


}